package com.blockmeta.bbs.businesslibrary.business;

import android.text.TextUtils;
import com.blankj.utilcode.util.LogUtils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.SecurityUtil;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.net.callback.CommonWebCallback;
import com.blockmeta.bbs.businesslibrary.net.pojo.BBSAuthorPOJO;
import com.blockmeta.bbs.businesslibrary.net.pojo.BBSCommentDetailPOJO;
import com.blockmeta.bbs.businesslibrary.net.pojo.BBSCommentPagePOJO;
import com.blockmeta.bbs.businesslibrary.net.pojo.BBSRewardAuthorPOJO;
import com.blockmeta.bbs.businesslibrary.net.pojo.BBSVoteAuthorPOJO;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RestManager;
import com.blockmeta.bbs.businesslibrary.net.retrofit.api.InterfaceBBS;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Retrofit;

import static com.blockmeta.bbs.businesslibrary.constant.Constant.MICRO_APP;


public class BBSManager extends BaseManager {


    public enum OrderType {
        UP(0, R.string.asc), Down(1, R.string.desc);

        int m_value;
        int m_strid;

        OrderType(int value, int strid) {
            m_value = value;
            m_strid = strid;
        }

        public int getValue() {
            return m_value;
        }


        public int getStrid() {
            return m_strid;
        }
    }

    public enum RequestType {
        ForWard(0), UpWard(1), ToPage(2), ReOrder(3);

        int m_value;

        RequestType(int value) {
            m_value = value;
        }

        public int getValue() {
            return m_value;
        }
    }

    public BBSManager() {
        super();
    }

    public void searchComments(String authorid, String id, int pageNo, OrderType orderType, final RequestType requestType) {

        Retrofit retrofit = RestManager.getRetrofit();

        InterfaceBBS comment = retrofit.create(InterfaceBBS.class);
        Call<String> call;
        authorid = TextUtils.isEmpty(authorid) ? null : authorid;
        String url;
        if (authorid==null) {
            url = MICRO_APP + "/bbs/comments?tid=" + id + "&uid=" + AccountHelper.getUid() + "&page=" + pageNo + "&ordertype=" + orderType.getValue();
        }else {
            url = MICRO_APP + "/bbs/comments?authorid=" + authorid + "&tid=" + id + "&uid=" + AccountHelper.getUid() + "&page=" + pageNo + "&ordertype=" + orderType.getValue();
        }
        String token = SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.TOKEN,"");
        String time = String.valueOf(System.currentTimeMillis()/1000);
        String sign = SecurityUtil.MD5(url+token+time);
        call = comment.getComments(authorid, id, AccountHelper.getUid(), pageNo, orderType.getValue(),sign,time);

        call.enqueue(new CommonWebCallback<String>("getComments") {

            @Override
            public void onSuccess(String response) {


                BBSCommentPagePOJO bbsCommentPagePOJO = new BBSCommentPagePOJO();
                List<BBSCommentDetailPOJO> commentDetailPOJOList = new ArrayList<>();

                JSONObject jsonObject1 = JSONObject.parseObject(response, Feature.OrderedField);
                JSONObject list1 = jsonObject1.getJSONObject("list");
                int totalpage = jsonObject1.getIntValue("totalpage");
                if (list1 != null) {
                    LinkedHashMap<String, BBSCommentDetailPOJO> jsonMap = JSON.parseObject(list1.toJSONString(), new TypeReference<LinkedHashMap<String, BBSCommentDetailPOJO>>() {
                    });
                    for (Map.Entry<String, BBSCommentDetailPOJO> entry : jsonMap.entrySet()) {
                        commentDetailPOJOList.add(entry.getValue());
                    }
                }
                bbsCommentPagePOJO.setTatal_page(totalpage);
                bbsCommentPagePOJO.setList(commentDetailPOJOList);
                post(new Event_Comments(true, bbsCommentPagePOJO, requestType));
            }

            @Override
            public void onError(Throwable t) {
                post(new Event_Comments(false, null, requestType));
            }
        });
    }
    public void searchComments(String authorid,String id, int pageNo, OrderType orderType, final RequestType requestType,int stand) {

        Retrofit retrofit = RestManager.getRetrofit();

        InterfaceBBS comment = retrofit.create(InterfaceBBS.class);
        Call<String> call;
        authorid = TextUtils.isEmpty(authorid) ? null : authorid;
        String url;
        if (authorid==null) {
            url = MICRO_APP + "/bbs/comments?tid=" + id + "&uid=" + AccountHelper.getUid() + "&page=" + pageNo + "&ordertype=" + orderType.getValue()+ "&stand=" + stand;
        }else {
            url = MICRO_APP + "/bbs/comments?authorid=" + authorid + "&tid=" + id + "&uid=" + AccountHelper.getUid() + "&page=" + pageNo + "&ordertype=" + orderType.getValue()+ "&stand=" + stand;
        }
        String token = SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.TOKEN,"");
        String time = String.valueOf(System.currentTimeMillis()/1000);
        String sign = SecurityUtil.MD5(url+token+time);
        call = comment.getComments(authorid, id, AccountHelper.getUid(), pageNo, orderType.getValue(),stand,sign,time);

        call.enqueue(new CommonWebCallback<String>("getComments") {

            @Override
            public void onSuccess(String response) {


                BBSCommentPagePOJO bbsCommentPagePOJO = new BBSCommentPagePOJO();
                List<BBSCommentDetailPOJO> commentDetailPOJOList = new ArrayList<>();

                JSONObject jsonObject1 = JSONObject.parseObject(response, Feature.OrderedField);
                JSONObject list1 = jsonObject1.getJSONObject("list");
                int totalpage = jsonObject1.getIntValue("totalpage");
                LinkedHashMap<String, BBSCommentDetailPOJO> jsonMap = JSON.parseObject(list1.toJSONString(), new TypeReference<LinkedHashMap<String, BBSCommentDetailPOJO>>() {
                });
                for (Map.Entry<String, BBSCommentDetailPOJO> entry : jsonMap.entrySet()) {
                    LogUtils.i("json", entry.getKey() + "----------" + entry.getValue());
                    commentDetailPOJOList.add(entry.getValue());
                }
                int size = commentDetailPOJOList.size();
                LogUtils.i("json", size + "---------size");
                bbsCommentPagePOJO.setTatal_page(totalpage);
                bbsCommentPagePOJO.setList(commentDetailPOJOList);
                post(new Event_Comments(true, bbsCommentPagePOJO, requestType));
            }

            @Override
            public void onError(Throwable t) {
                post(new Event_Comments(false, null, requestType));
            }
        });
    }


    public void getAuthor(String tid, String uid) {
        Retrofit retrofit = RestManager.getRetrofit();

        InterfaceBBS comment = retrofit.create(InterfaceBBS.class);
        String url = "/bbs?tid="+tid+"&uid="+uid;
        String token = SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.TOKEN,"");
        String time = String.valueOf(System.currentTimeMillis()/1000);
        String sign = SecurityUtil.MD5(url+token+time);
        Call<String> call = comment.getAuthor(tid, uid,sign,time);
        call.enqueue(new CommonWebCallback<String>("getAuthor") {
            @Override
            public void onSuccess(String response) {

                BBSAuthorPOJO detailPOJO = GsonUtil.buildGson().fromJson(response, BBSAuthorPOJO.class);
                if (detailPOJO != null) {
                    post(new Event_Author(true, detailPOJO));
                } else {
                    post(new Event_Author(false, detailPOJO));
                }
            }

            @Override
            public void onError(Throwable t) {
                post(new Event_Author(false, null));
            }
        });
    }

    /**
     * 悬赏
     *
     * @param tid
     * @param uid
     */
    public void getRewardAuthor(String tid, String uid) {

        Retrofit retrofit = RestManager.getRetrofit();

        InterfaceBBS comment = retrofit.create(InterfaceBBS.class);
        String url = "/bbs?tid="+tid+"&uid="+uid;
        String token = SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.TOKEN,"");
        String time = String.valueOf(System.currentTimeMillis()/1000);
        String sign = SecurityUtil.MD5(url+token+time);
        Call<String> call = comment.getAuthor(tid, uid,sign,time);

        call.enqueue(new CommonWebCallback<String>("getAuthor") {

             @Override
            public void onSuccess(String response) {

                BBSRewardAuthorPOJO detailPOJO = GsonUtil.buildGson().fromJson(response, BBSRewardAuthorPOJO.class);
                if (detailPOJO != null) {
                    post(new Event_Reward(true, detailPOJO));
                } else {
                    post(new Event_Reward(false, detailPOJO));
                }
            }

            @Override
            public void onError(Throwable t) {
                post(new Event_Reward(false, null));
            }
        });

    }

    /**
     * 投票
     *
     * @param tid
     * @param uid
     */
    public void getVoteAuthor(String tid, String uid) {

        Retrofit retrofit = RestManager.getRetrofit();

        InterfaceBBS comment = retrofit.create(InterfaceBBS.class);
        String url = "/bbs?tid="+tid+"&uid="+uid;
        String token = SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.TOKEN,"");
        String time = String.valueOf(System.currentTimeMillis()/1000);
        String sign = SecurityUtil.MD5(url+token+time);
        Call<String> call = comment.getAuthor(tid, uid,sign,time);

        call.enqueue(new CommonWebCallback<String>("getAuthor") {

            @Override
            public void onSuccess(String response) {

                BBSVoteAuthorPOJO detailPOJO = GsonUtil.buildGson().fromJson(response, BBSVoteAuthorPOJO.class);
                if (detailPOJO != null) {
                    post(new Event_Vote(true, detailPOJO));
                } else {
                    post(new Event_Vote(false, detailPOJO));
                }
            }

            @Override
            public void onError(Throwable t) {
                post(new Event_Vote(false, null));
            }
        });
    }

    public static class Event_Comments {
        public boolean isSuccess;
        public BBSCommentPagePOJO m_bbsCommentPagePOJO;
        public RequestType m_requestType;

        public Event_Comments(boolean isSuccess, BBSCommentPagePOJO bbsCommentPagePOJO, RequestType requestType) {
            this.isSuccess = isSuccess;
            this.m_bbsCommentPagePOJO = bbsCommentPagePOJO;
            m_requestType = requestType;
        }
    }

    /**
     * 帖子详情
     */
    public static class Event_Author {
        public boolean isSuccess;
        public BBSAuthorPOJO m_bbsAuthorPOJO;

        public Event_Author(boolean isSuccess, BBSAuthorPOJO bbsAuthorPOJO) {
            this.isSuccess = isSuccess;
            this.m_bbsAuthorPOJO = bbsAuthorPOJO;
        }
    }

    /**
     * 悬赏
     */
    public static class Event_Reward {
        public boolean isSuccess;
        public BBSRewardAuthorPOJO bbsrewardPOJO;

        public Event_Reward(boolean isSuccess, BBSRewardAuthorPOJO bbsrewardPOJO) {
            this.isSuccess = isSuccess;
            this.bbsrewardPOJO = bbsrewardPOJO;
        }
    }

    public static class Event_Vote {
        public boolean isSuccess;
        public BBSVoteAuthorPOJO mBBSVoteAuthorPOJO;

        public Event_Vote(boolean isSuccess, BBSVoteAuthorPOJO bbsVoteAuthorPOJO) {
            this.isSuccess = isSuccess;
            this.mBBSVoteAuthorPOJO = bbsVoteAuthorPOJO;
        }
    }
}
